package com.hjq.mallproduct.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.hjq.common.validator.group.AddGroup;
import com.hjq.common.validator.group.UpdateGroup;
import com.hjq.mallproduct.entity.vo.SpuSaveVO;
import com.hjq.mallproduct.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hjq.mallproduct.entity.SpuInfoEntity;
import com.hjq.mallproduct.service.SpuInfoService;
import com.hjq.common.utils.PageUtils;
import com.hjq.common.utils.R;

import javax.validation.constraints.NotBlank;


/**
 * spu信息
 *
 * @author Hjq
 * @email 1484397309@qq.com.com
 * @date 2022-05-24 14:18:32
 */
@RestController
@RequestMapping("mallproduct/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;
    @Autowired
    private BrandService brandService;

    @RequestMapping("/getSpuInfo")
    public R getSpuInfoBySkuId(@RequestParam("skuId") Long skuId){
        SpuInfoEntity spuInfoEntity=spuInfoService.getSpuInfoBySkuId(skuId);
        String name = brandService.getById(spuInfoEntity.getBrandId()).getName();
        return R.ok().put("data",spuInfoEntity).put("brand",name);
    }
    /**
     * 上架商品，将商品信息传进ES
     */
    @RequestMapping("/{spuId}/up")
    //@RequiresPermissions("mallproduct:skuinfo:list")
    public R upSpu(@PathVariable("spuId") Long spuId){
        spuInfoService.upSpuInfoToES(spuId);

        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("mallproduct:spuinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = spuInfoService.queryPageByCondition(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("mallproduct:spuinfo:info")
    public R info(@PathVariable("id") Long id){
		SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("mallproduct:spuinfo:save")
    public R save(@RequestBody SpuSaveVO spuInfo){
		spuInfoService.saveSpuInfo(spuInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("mallproduct:spuinfo:update")
    public R update(@RequestBody SpuInfoEntity spuInfo){
		spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("mallproduct:spuinfo:delete")
    public R delete(@RequestBody Long[] ids){
		spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
